<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/12/20
 * Time: 14:09
 */

namespace app\sports_admin\controller;
use app\sports_admin\model\DataCategory;
use app\sports_admin\model\DataBook;
use think\db;
use think\Request;
use app\sports_admin\service\Book as BookService;
use app\exception\BookException;

class Book extends Base
{
    protected $beforeActionList =[
        'isResource'=>['only'=>'addpage,add,del,uppage,update'],
        'operationSelect'=>['only'=>'listpage,addpage,booklist,type,uppage'],
        'operationDelete'=>['only'=>'del'],
        'operationUpdate'=>['only'=>'update']
    ];
    //跳转到图书列表页面
    public function listPage()
    {
        $type = DataCategory::field("id,level,pid,catname")->select();

        return view("list",compact("type"));
    }
    //跳转到添加图书信息的页面
    public function addPage()
    {
        $type = DataCategory::field("id,level,pid,catname")->select();
        return view("add",compact("type"));
    }
    //添加图书
    public function add(Request $request){
        if($request->isAjax()){
            $data =  DataBook::add();
            if(!$data){
                throw new BookException(['msg'=>'添加失败', 'errorCode'=>203]);
            }
                return json(['code'=>200,'msg'=>'添加成功']
            );
        }
    }
    public function bookList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName','searchType']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'school_id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $searchType = trim($params['searchType']);
        $result = DataBook::getBooks($page,$rows,$sort,$sortOrder,$searchName,$searchType);
        return $result;
    }
    //
    public function type()
    {
        $id = input('get.id');
            $data = DataCategory::where("pid",$id)->select();
            if($data){
                return json(['data'=>$data]);
            }else{
                throw new BookException(['msg'=>'找不到数据', 'errorCode'=>"404"]);
            }
    }
    //删除图书
    public function del(Request $request)
    {
        if($request->isAjax()){
            $data = BookService::del();
            if(!$data){
                throw new BookException(['msg'=>'删除失败', 'errorCode'=>"404"]);
            }
            return json(['code'=>200,'msg'=>'删除成功']);
        }
    }
    //编辑图书内容页面
    public function upPage()
    {
        $id   = Request::instance()->only("id");
        $data = BookService::content($id);
        $type = DataCategory::field("id,level,pid,catname")->select();
        return view("update",compact("type","data"));
    }
    //编辑内容
    public function update(Request $request)
    {
        if($request->isAjax()){
            $data = BookService::update();
            if(!$data){
                return json(['msg'=>'修改失败', 'errorCode'=>203]);
            }
            return json(['code'=>200,'msg'=>'修改成功']);
        }
    }


}